package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_CROSS_TRADE_WAR_PREPARE_QUERY_NOTIFY, desc = "跨服商战匹配查询通知")
public class CrossTradeWarPrepareQueryNotifyCmd extends CrossUnionCommand {
    private CrossTradeWarProto.CrossTradeWarPrepareRespMsg reqMsg;
    private long myUserId;
    private long myServerId;
    private String myUnionId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossTradeWarProto.CrossTradeWarPrepareRespMsg.parseFrom(packet.getBytes());
        myUserId = reqMsg.getMyUserId();
        myServerId = reqMsg.getMyServerId();
        myUnionId = reqMsg.getMyUnionId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        YanQuMessage pbMsg;
        if(0 == reqMsg.getRet() && reqMsg.getBattleType() == eTradeWarBattleType.Track.getValue()){
            int groupId = Cross2UnionActivityGroupMgr.getGroupId(reqMsg.getActivityId(),reqMsg.getMyUserId());
            ret = CrossTradeWarMgr.crossTrack(reqMsg.getActivityId(), reqMsg.getBattleUid(), reqMsg.getMyUserId(), reqMsg.getMyServerId(), myUnionId);
            if(groupId == UnionActivityGroupHelper.NO_GROUP_ID ||0 != ret){
                CrossTradeWarProto.CrossTradeWarPrepareRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPrepareRespMsg.newBuilder();
                respMsg.setActivityId(reqMsg.getActivityId());
                respMsg.setMyUserId(myUserId);
                respMsg.setMyServerId(myServerId);
                respMsg.setRet(ret);
                respMsg.setBattleType(reqMsg.getBattleType());
                respMsg.setMyUnionId(myUnionId);
                pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PREPARE_NOTIFY, respMsg);
            }else {
                int wantedItemId = 0;
                long publishWantedUserId = 0;
                TradeWarWanted tradeWarWanted = CrossTradeWarMgr.getTradeWarWanted(reqMsg.getActivityId(), reqMsg.getBattleUid(),groupId);
                if(null != tradeWarWanted){
                    wantedItemId = tradeWarWanted.getUseItemId();
                    publishWantedUserId = tradeWarWanted.getUserId();
                }
                CrossTradeWarProto.CrossTradeWarPrepareRespMsg.Builder respMsg = reqMsg.toBuilder();
                respMsg.setWantedItemId(wantedItemId);
                respMsg.setPublishWantedUserId(publishWantedUserId);
                pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PREPARE_NOTIFY, respMsg);
            }
        }else {
            pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PREPARE_NOTIFY, reqMsg);
        }
        MessageHelper.sendPacket(myServerId, myUserId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
